package com.warmlight.voicepacket.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.TempData;
import com.warmlight.voicepacket.data.eventbus.PlayerServiceData;
import com.warmlight.voicepacket.utils.SPUtils;
import com.warmlight.voicepacket.utils.ShareCollectCallBackUtils;
import com.warmlight.voicepacket.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * 分享音频到微信qq 播放，弹框页面
 */
@SuppressLint("ValidFragment")
public class CopyShareFragment extends BottomSheetDialogFragment implements View.OnClickListener{
    private SeekBar sb_delay_time;
    private TextView tv_audio_delay_time;

    public interface ShareClick{
        void click();
    }

    private ShareClick shareClick;

    public void setOnShareClick(ShareClick shareClick){
        this.shareClick = shareClick;
    }

    @SuppressLint("ValidFragment")
    public CopyShareFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layout_copyshare, container, false);
        initView(v);
        return v;
    }
    private void initView(View v){
        View ll_sharewx = v.findViewById(R.id.ll_sharewx);
        View ll_shareqq = v.findViewById(R.id.ll_shareqq);
        View ll_sharewb = v.findViewById(R.id.ll_sharewb);
        tv_audio_delay_time = (TextView) v.findViewById(R.id.tv_audio_delay_time);
        sb_delay_time = (SeekBar) v.findViewById(R.id.sb_delay_time);
        sb_delay_time.setMax(10);
        sb_delay_time.setProgress((int) SPUtils.getVoiceDelayTime() / 1000);
        tv_audio_delay_time.setText((int)SPUtils.getVoiceDelayTime() / 1000 + "s");

        sb_delay_time.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                tv_audio_delay_time.setText(seekBar.getProgress()+"s");
                SPUtils.setVoiceDelayTime(seekBar.getProgress() * 1000L);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        ll_sharewx.setOnClickListener(this);
        ll_shareqq.setOnClickListener(this);
        ll_sharewb.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        if(TempData.AudioData!=null){
            EventBus.getDefault().post(new PlayerServiceData(PlayerServiceData.STOP, "stop"));
            EventBus.getDefault().post(new PlayerServiceData(PlayerServiceData.PLAY, TempData.AudioData.getUrl(),"delay",false));
        }
        ShareCollectCallBackUtils.shareCallBack(TempData.AudioData.getId(),"1");
        if (shareClick != null){
            shareClick.click();
        }
        switch (view.getId()){
            case R.id.ll_sharewx:
//                Log.e("onClick: '",Wechat.NAME );
                share("com.tencent.mm");
                dismiss();
                break;
            case R.id.ll_shareqq:
                share("com.tencent.mobileqq");
                dismiss();
                break;
            case R.id.ll_sharewb:
                share("com.sina.weibo");
                dismiss();
                break;

        }

    }
    private void share(String pn){

        // 通过包名获取要跳转的app，创建intent对象

        Intent intent = getContext().getPackageManager().getLaunchIntentForPackage(pn);

// 这里如果intent为空，就说名没有安装要跳转的应用嘛

        if (intent != null) {

            // 这里跟Activity传递参数一样的嘛，不要担心怎么传递参数，还有接收参数也是跟Activity和Activity传参数一样

            intent.putExtra("name", "Liu xiang");

            intent.putExtra("birthday", "1983-7-13");

            startActivity(intent);

        } else {

            // 没有安装要跳转的app应用，提醒一下

            ToastUtil.showShort(getContext(), "哟，赶紧下载安装这个APP吧");

        }
    }
}